﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using tnet.Controls.Lookless;
using tnet.Model;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;

namespace tnet
{
    public partial class MainPage : PhoneApplicationPage
    {  
        private void TopNewsItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        { 
            _Vibration.Start(_ThisTime);

            TextBlock thisGrid = sender as TextBlock;
            String Temp = thisGrid.Tag.ToString();
             
            if (!(String.IsNullOrEmpty(Temp)))
            { 
                Tools.SaveSetting("TempNewsItem", Temp);

                NavigationService.Navigate(new Uri("/TempNewsItem.xaml", UriKind.Relative)); 
            } 
        } 

        private void CallGlenn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _Vibration.Start(_ThisTime);
            Tools.LaunchURL(_RootURL);
             
            //ExecuteCall(_PhoneNumber);
        }

        private void ImageURL_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        { 
            _Vibration.Start(_ThisTime);

            // do something with tag/url
            Image ThisImage = sender as Image;

            if (!(String.IsNullOrEmpty(ThisImage.Tag.ToString())))
            {
                Tools.LaunchURL(ThisImage.Tag.ToString());
            }
        }

        private void NewsRSS_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            try
            {
                ListBox ThisBox = sender as ListBox;
                RssItem ThisItem = ThisBox.SelectedItem as RssItem;
                 
                Tools.SaveSetting("TempNewsItem", ThisItem.FreezeDried);

                NavigationService.Navigate(new Uri("/TempNewsItem.xaml", UriKind.Relative));
            }
            catch (Exception) { } 
        }

        private void RSSListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            ListBox ThisBox = sender as ListBox;
            RssItem ThisItem = ThisBox.SelectedItem as RssItem;
            if (ThisItem != null)
            {
                if (!(String.IsNullOrEmpty(ThisItem.Link.ToString())))
                {
                    Tools.LaunchURL(ThisItem.Link.ToString());
                }
            }
        } 

        private void EmailNews_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _Vibration.Start(_ThisTime);
             
            Tools.SendEmail(String.Empty, _EmailSubject, _HomeEmail);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            ShellTile SecondTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("/MainPage.xaml?Task=2"));

            StandardTileData NewTileData = new StandardTileData();

            NewTileData.Title = _CallTileText;
            NewTileData.BackgroundImage = new Uri("Resources/speeddial.jpg", UriKind.Relative);
            NewTileData.Count = 0;
            NewTileData.BackTitle = "";
            NewTileData.BackBackgroundImage = new Uri("Resources/Icon.Daily.jpg", UriKind.Relative);

            if (SecondTile == null)
            {
                ShellTile.Create(new Uri("/MainPage.xaml?Task=2", UriKind.Relative), NewTileData);
            }
            else
            {
                SecondTile.Update(NewTileData);
            }
        }

        private void RateIcn_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);

            MarketplaceReviewTask ThisItem = new MarketplaceReviewTask();
            ThisItem.Show();
        }

        private void GlennIcn_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);
             
            Tools.SendEmail(String.Empty, _EmailSubject, _WorkEmail);
        }

        private void PreloadAnimation_Completed(object sender, EventArgs e)
        {
            // rss processing
            String LastDateValue = Tools.GetSetting("LastDateValue");
            if (!(String.IsNullOrEmpty(LastDateValue)))
            {
                ProcessXMLResults(Tools.GetSetting("LastDateResults.XML"));
            }

            // twitter 
            //String LastDateValueTwitter = Tools.GetSetting("LastDateValueTwitter");
            //if (!(String.IsNullOrEmpty(LastDateValueTwitter)))
            //{
            //    ProcessXMLTwitterResults(Tools.GetSetting("LastDateResultsTwitter.XML"));
            //}
        }
         
        private void help_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime); 

            Tools.SendEmail(String.Empty, _AdminEmailSubject, _AdminEmail);
        }

        private void TheBlazeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            MenuItem ThisItem = sender as MenuItem;
            String FreezeDriedItem = ThisItem.Tag.ToString();
            String[] FreezeDriedItemArray = FreezeDriedItem.Split('^');

            // save data...
            String FavStoryKey = Tools.ScrubTitleToKey(FreezeDriedItemArray[0]) + ".fav";
            Tools.SaveSetting(FavStoryKey, FreezeDriedItem);

            // pin news item
            ShellTile SecondTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("/NewsItem.xaml?FD=" + FavStoryKey));

            StandardTileData NewTileData = new StandardTileData();

            NewTileData.Title = FreezeDriedItemArray[0];
            NewTileData.BackgroundImage = new Uri("Resources/News.jpg", UriKind.Relative);
            NewTileData.Count = 0;
            NewTileData.BackTitle = _BackTileTitle;
            NewTileData.BackBackgroundImage = new Uri("Resources/Icon.Daily.jpg", UriKind.Relative);

            if (SecondTile == null)
            {
                ShellTile.Create(new Uri("/NewsItem.xaml?FD=" + FavStoryKey, UriKind.Relative), NewTileData);
            }
            else
            {
                SecondTile.Update(NewTileData);
            }

            SaveToFavorites(FavStoryKey);
        }

        private void AddToFavoritesMenuItem_Click(object sender, RoutedEventArgs e)
        { 
            _Vibration.Start(_ThisTime);

            MenuItem ThisItem = sender as MenuItem;
            String FreezeDriedItem = ThisItem.Tag.ToString();
            String[] FreezeDriedItemArray = FreezeDriedItem.Split('^');

            // save data...
            String FavStoryKey = Tools.ScrubTitleToKey(FreezeDriedItemArray[0]) + ".fav";
            Tools.SaveSetting(FavStoryKey, FreezeDriedItem);

            // add to favorites 
            SaveToFavorites(FavStoryKey);
        }

        private void Favorites_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);

            NavigationService.Navigate(new Uri("/MyFavorites.xaml", UriKind.Relative));
        }
    }
}
